package com.spark.lola.controller.sup;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.support.mybatis.common.QueryCondition;
import com.spark.lola.support.wms.configuration.WmsProperties;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.entity.sup.BankInfo;
import com.spark.lola.mapper.sup.BankInfoMapper;
import com.spark.lola.service.sup.IBankInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengyejun
 * @since 2020-04-24
 */
@RestController
@RequestMapping("/api/sup/bank-info")
public class BankInfoController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(BankInfoController.class);
    @Autowired
    private BankInfoMapper currentMapper;
    @Autowired
    private IBankInfoService currentService;
    @Autowired
    private WmsProperties wmsProperties;

    @RequestMapping(value = "map/{supplierId}")
    public ResponseBean map(@PathVariable Long supplierId) {
        return ResponseBean.success(currentService.findBySupplierId(supplierId));
    }

    @RequiresPermissions(value = "m5230")
    @RequestMapping(value = "/findBy", consumes = "application/json")
    public ResponseBean findBy(@RequestBody QueryParam<BankInfo> queryBean,
                               @SessionAttribute ShiroUser shiroUser) {
        if(wmsProperties.getPurchase().getDutyId().get(1).equals(shiroUser.getDutyId())) {
            QueryCondition queryCondition = new QueryCondition();
            queryCondition.setKey("s.salesmanId");
            queryCondition.setOper("=");
            queryCondition.setValue(shiroUser.getId());
            queryBean.getQueryList().add(queryCondition);
        }
        PageParam<BankInfo> pageParam = queryBean.toPageParam();
        IPage<BankInfo> page = currentMapper.page(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @RequiresPermissions(value = "m5234")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable Long id,
                                 @SessionAttribute ShiroUser shiroUser) {
        BankInfo model;
        if (id == 0) {
            model = new BankInfo();
        } else {
            model = currentService.getById(id);
        }
        return ResponseBean.success(model);
    }

    @RequiresPermissions(value = {"m5231", "m5233"}, logical = Logical.OR)
    @RequestMapping(value = "/save", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody BankInfo form, @SessionAttribute ShiroUser shiroUser) throws Exception {
        BankInfo model = new BankInfo();
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = currentService.getById(form.getId());
        }
        DataBeanUtils.buildUpdateBean(model, form);
        currentService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @RequiresPermissions(value = "m5235")
    @RequestMapping("/on")
    public ResponseBean on(@RequestParam String nos) {
        int count = currentService.updateUsedByIds(CommonConstant.Y, nos);
        return ResponseBean.success(null, count);
    }

    @RequiresPermissions(value = "m5236")
    @RequestMapping("/un")
    public ResponseBean un(@RequestParam String nos) {
        int count = currentService.updateUsedByIds(CommonConstant.N, nos);
        return ResponseBean.success(null, count);
    }

    @RequiresPermissions(value = "m5232")
    @RequestMapping("/del")
    public ResponseBean del(@RequestParam(value = "nos") List<Long> ids) {
        ids.forEach(id -> {
            currentService.removeById(id);
        });
        return ResponseBean.success();
    }
}
